package com.glela.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.api.upload.qiniu.general.QiniuUtils;
import com.glela.common.util.Base64;
import com.glela.goods.model.request.GoodsCommentRequest;
import com.glela.goods.model.request.ImgBasePathRequest;
import com.glela.goods.model.request.RequestGoodsCommenntBaseModel;
import com.glela.goods.model.request.RequestGoodsCommontModel;
import com.glela.order.service.GoodsCommentService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping("goodsComment")
public class GoodsCommentController {

	private static Logger logger = LoggerFactory.getLogger(GoodsCommentController.class);

	@Autowired
	private GoodsCommentService goodsCommentService;

	//{"imgBase":"byte64","suffix":"jpg"}
	@RequestMapping(value = "/imgBasePath", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> imgBasePath(@RequestBody ImgBasePathRequest request){
		try {
			if (ParameterUtils.validParams(request.getImgBase())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getSuffix())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			byte[] b = Base64.decode(request.getImgBase()) ;
			String imgurl = QiniuUtils.uploadByBytys64("file/goodsComment/", b, request.getSuffix());
			if ("".equals(imgurl)) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29907), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<String>(imgurl), HttpStatus.OK);
			
		}catch(Exception e) {
			// TODO: handle exception
			logger.error("上传图片异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 商品评价
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addGoodsComment", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> newaddGoodsComment(@RequestBody RequestGoodsCommontModel model) {
		try {
			if(ParameterUtils.validParams(model.getUserId())){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			List<RequestGoodsCommenntBaseModel> models = model.getComment();
			// 没有内容时
			if(models.size() < 0){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			// 评论内容和图片1都为空时
			if(models.get(0).getCommentText().trim() == null && models.get(0).getImage1() == null){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return goodsCommentService.increaseTheCommodityComments(model.getUserId(), model.getOrderInfoId(), model.getComment());
		} catch (Exception e) {
			logger.error("添加商品评论", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 返回商品评论列表 {"pageSize":10,"pageNum":1,"goodId":1}
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/listGoodsCommentByGoodId", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> listGoodsCommentByGoodId(@RequestBody GoodsCommentRequest request) {
		try {
			if (ParameterUtils.validParams(request.getPageNum())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(goodsCommentService.listGoodsCommentByGoodId(request)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("返回商品评论列表异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 查询可评论的商品列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listCanCommentOrder", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> listCanCommentOrder(@RequestBody RequestGoodsCommontModel model){
		try {
			if(ParameterUtils.validParams(model.getUserId(),model.getOrderInfoId())){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return goodsCommentService.findCanCommentOrderList(model.getUserId(), model.getOrderInfoId());
		} catch (Exception e) {
			logger.error("返回商品评论列表异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

}
